package com.ohc.leetcode.链表;

import java.util.*;

/**
 * 2022年6月17日09:31:34
 * 过
 */
public class 相交链表160 {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    /**
     * 题解 学习
     *
     * @param headA
     * @param headB
     * @return
     */
    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        Map<ListNode, Integer> map = new HashMap<>();
        while (headA != null) {
            map.put(headA, 1);
            headA = headA.next;
        }

        while (headB != null) {
            Integer integer = map.get(headB);
            if (integer != null) {
                return headB;
            }
        }
        return null;
    }

    /**
     * 题解学习
     *
     * @param headA
     * @param headB
     * @return
     */
    public static ListNode getIntersectionNode2(ListNode headA, ListNode headB) {
        Stack<ListNode> stack1 = new Stack<>();
        Stack<ListNode> stack2 = new Stack<>();
        while (headA != null) {
            stack1.push(headA);
        }
        while (headB != null) {
            stack2.push(headB);
        }
        ListNode temp = null;
        while (!stack1.isEmpty() && !stack2.isEmpty()) {
            ListNode pop1 = stack1.pop();
            ListNode pop2 = stack2.pop();
            temp = pop1;
            if (!pop1.equals(pop2)) {
                break;
            }
        }
        return temp;
    }

    public static void main(String[] args) {
        Integer integer = Integer.valueOf("101", 2);
        System.out.println("integer = " + integer);
    }
}
